// 简单TCP服务器
#include <stdio.h>
#include <stdlib.h>
#include <WinSock2.h>

#pragma comment(lib, "ws2_32.lib")

const int BUFSIZE = 20;

int main()
{
    WSAData data;
    WSAStartup(1, &data);

    int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    struct sockaddr_in servAddr;
    memset(&servAddr, 0, sizeof(servAddr));
    servAddr.sin_family = AF_INET;
    servAddr.sin_port = htons(8080);
    servAddr.sin_addr.s_addr = htonl(INADDR_ANY);

    bind(s, (struct sockaddr *)&servAddr, sizeof(servAddr));

    listen(s, 100);

    struct sockaddr_in client;
    int len = sizeof(client);
    SOCKET clientSock = accept(s, (struct sockaddr *)&client, &len);
    printf("连接成功！\n");

    char buf[BUFSIZE] = {0};
    send(clientSock, buf, strlen(buf), 0);
    recv(clientSock, buf, sizeof(buf) - 1, 0);
    printf("%s\n", buf);

    closesocket(s);
    WSACleanup();
    return 0;
}
